package com.cmc.cloud.cmclink.doc.service.impl.so;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoScnErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoScnConvert;
import com.cmc.cloud.cmclink.doc.entity.SoScnDO;
import com.cmc.cloud.cmclink.doc.mapper.SoScnMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoScnService;
import com.cmc.cloud.cmclink.doc.vo.sovo.soscnvo.SoScnCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.soscnvo.SoScnPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.soscnvo.SoScnRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.soscnvo.SoScnUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 订舱相关方信息 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoScnServiceImpl implements SoScnService {

    @Resource
    private SoScnMapper soScnMapper;

    @Override
    public void createSoScn(SoScnCreateReqVO createReqVO) {
        // 插入
        SoScnDO soScn = SoScnConvert.INSTANCE.convert(createReqVO);
        soScnMapper.insert(soScn);
    }

    @Override
    public SoScnDO createOrUpdateSo(Long soId, SoScnUpdateReqVO createReqVO, SoScnRespVO soScnOld) {
        // 插入
        SoScnDO soScn = SoScnConvert.INSTANCE.convert(createReqVO);
        if (createReqVO.getId() != null) {
            // 数据一致性校验
            if (!Objects.equals(createReqVO.getId(), soScnOld.getId())) {
                throw ServiceExceptionUtil.exception(SoScnErrorCodeConstants.SO_SCN_NOT_EXISTS);
            }
            if (!Objects.equals(createReqVO.getSoId(), soScnOld.getSoId())) {
                throw ServiceExceptionUtil.exception(SoScnErrorCodeConstants.SO_SCN_NOT_EXISTS);
            }
            soScnMapper.updateById(soScn);
        } else {
            soScn.setSoId(soId);
            soScnMapper.insert(soScn);
        }
        return soScn;
    }

    @Override
    public void updateSoScn(SoScnUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoScnExists(updateReqVO.getId());
        // 更新
        SoScnDO updateObj = SoScnConvert.INSTANCE.convert(updateReqVO);
        soScnMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoScn(Long id) {
        // 校验存在
        validateSoScnExists(id);
        // 删除
        soScnMapper.deleteById(id);
    }

    private void validateSoScnExists(Long id) {
        if (soScnMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoScnErrorCodeConstants.SO_SCN_NOT_EXISTS);
        }
    }

    @Override
    public SoScnDO getSoScn(Long id) {
        return soScnMapper.selectById(id);
    }

    @Override
    public List<SoScnDO> getSoScnList(Collection<Long> ids) {
        return soScnMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SoScnDO> getSoScnPage(SoScnPageReqVO reqVO) {
        LambdaQueryWrapperX<SoScnDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soScnMapper.selectPage(reqVO, queryWrapper);
    }

    public void deleteBySoIds(Collection<Long> sIds) {
        LambdaUpdateWrapper<SoScnDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SoScnDO::getSoId, sIds);
        soScnMapper.delete(updateWrapper);
    }

    @Override
    public SoScnDO seleteBySoId(Long soId) {
        LambdaQueryWrapper<SoScnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SoScnDO::getSoId, soId);
        queryWrapper.orderByAsc(SoScnDO::getId);
        return soScnMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<Long, SoScnDO> selectBySoIds(List<Long> soIds) {
        LambdaQueryWrapper<SoScnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SoScnDO::getSoId, soIds);
        return soScnMapper.selectList(queryWrapper).stream().collect(Collectors.toMap(SoScnDO::getSoId, e -> e));
    }

}
